console.log("开始打包热更新资源");
 
var archiver = require('archiver');
const os = require('os');
let fs = require("fs");
const exec = require('child_process').execSync;

function add_version(cur_version){
    const vers = cur_version.split(".");
    let pre = parseInt(vers[0]);
    let after = parseInt(vers[1]);

    if(after + 1 <= 9){
        after = after + 1;
    }else{
        pre = pre + 1;
        after = 0;
    }

    const final_version = `${pre}.${after}`;

    console.log("当前的版本号 ", cur_version, "添加后的版本号= ",final_version);
    return final_version;
}

/**@description 热更新的参数 */
const update_argv = {};     


/**@description true: 当前的参数表示资源是需要压缩过的 false: 表示不需要压缩 */
update_argv.compress = false;
/**@description 当前打包的版本号 */
update_argv.version = "1.0";
/**@description pack_type: release / debug 模式 */
update_argv.pack_type = "debug";
/**@description 渠道名称 */
update_argv.channel = "company";
/**@description 当前游戏的分之(游戏所在的分之名称) */
update_argv.branch = "Restaurant";
/**@description 打包的本地地址 */
update_argv.local_pack = "pack_Restaurant"


/**@description 更新热更新的参数 */
function init_update_argv(){
    const all_object = Object.keys(update_argv);
    for(const param_str of process.argv){
        for(const build_key of all_object){
            if(param_str.indexOf(build_key) == 0){
                console.log("build_key = ",build_key);
                const cte = param_str.split(":");
                if(cte[1]== "false"){
                    update_argv[build_key] = false;
                }else if(cte[0] == build_key){
                    update_argv[build_key] = cte[1];
                }
            }
        }
    }
    console.log("当前打包热更新的版本信息 = ", update_argv);
}

init_update_argv();

function execute(cmd) {
    exec(cmd, { stdio: 'inherit' });
}

function get_update_argv(){
    return update_argv;
}

 

/** * 是否为mac系统（包含iphone手机） * */ 
var isMac = function() { 
    let sysType = os.type();
    if(sysType==="Windows_NT"){
        return false;
        }else{
            return true;
        }
 };
    


/**@description 打包热更新的资源 */
function pack_assetes_update_res(){
    console.log("打包资源更新开始");
    const argv = get_update_argv();
    const branch_path = `https://bubuduo-guesssong.oss-cn-beijing.aliyuncs.com/games_assets_update/channel/${argv.channel}/${argv.pack_type}/${argv.branch}/`;
    const pack_res = `node version_generator.js -v ${argv.version} -u ${branch_path} -s ./../../${update_argv.local_pack}/ -d ./ -buildargv ${JSON.stringify(argv)}`;
    execute(pack_res);
    console.log("打包资源更新完成");
}


/**@description 上传热更新资源 */
function upload_files(){
    const argv = get_update_argv();
    console.log("开始打上传打包资源");
    const is_mac = isMac() ? "./ossutilmac64":"ossutil64.exe";
    const  manifest_path = `oss://bubuduo-guesssong/games_assets_update/channel/${update_argv.channel}/${update_argv.pack_type}/${update_argv.branch}/versions/${update_argv.version}/assert_update/remote-assets/`;
    const  pack_path  = `${manifest_path}`;
    const mac = is_mac
    console.log("\n热更新版本地址 = ", pack_path);
    // assets 
    const res_path = `./../../${update_argv.local_pack}/new_android_sdk/`;
    const all_remote_res = [
        "assets",
        "src",
    ];

    for(const assets  of all_remote_res){
        const upload_res_import = `${mac} cp ${res_path}/${assets} ${pack_path}${assets}/ --recursive -f -u --config-file ./.ossutilconfig`;
        execute(upload_res_import);
    }

    // 上传热更新的资源信息到OSS
    const all_assets_updates_res = [
        "project.manifest",
        "version.manifest",
    ]

    // 上传热更新的版本信息 和 文件信息
    const remote_manifest1 = `oss://bubuduo-guesssong/games_assets_update/channel/${argv.channel}/${argv.pack_type}/${argv.branch}`;
    for(const assets of all_assets_updates_res){
        const upload_res_import = `${mac} cp ./${assets} ${remote_manifest1}/ --recursive -f -u --config-file ./.ossutilconfig`;
        execute(upload_res_import);
    }

    // 把本地的文件信息也上传一次到文件的
    const local_project_manifest_url = `oss://bubuduo-guesssong/games_assets_update/channel/${argv.channel}/${argv.pack_type}/${argv.branch}/local_manifest/${argv.version}`;
    const upload_res_import = `${mac} cp ./project.manifest ${local_project_manifest_url}/ --recursive -f -u --config-file ./.ossutilconfig`;
    execute(upload_res_import);
    console.log("开始打上传打包资源");
}


/**@description 开始热更新 */
function start_update(){
    pack_assetes_update_res();
    upload_files();
}

start_update();


